package com.css.apps.base.user.action.login;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.util.MenuUtil;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.spi.IUserListener;
import com.css.common.PluginBus;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.UserAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class ChangeLoginDept extends UserAction{
	private static Log log = LogFactory.getLog(ChangeLoginDept.class);
	private String userId;
	private String deptId;
	public SUser sUser = new SUser();
	@SuppressWarnings("deprecation")
	protected String userGo() {
		TransactionCache tx = null;
		try{
			sUser = QueryCache.get(SUser.class, userId);
			tx = new TransactionCache();
			sUser.setOrgId(deptId);
			tx.update(sUser);
			tx.commit();
			IUserListener lis=PluginBus.getPlugin(IUserListener.class);
			if(lis!=null){
				lis.afterLogin(sUser);
			}	
			set(Environment.SESSION_LOGIN_KEY, sUser);
			MenuUtil.refreshMenu();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			if(tx != null){
				tx.rollback();
				tx = null;
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}
	
	public String getDeptId() {
		return deptId;
	}
	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public SUser getsUser() {
		return sUser;
	}
	public void setsUser(SUser sUser) {
		this.sUser = sUser;
	}
	
}
